/*
Filename: UCDeleteAdmin.java
Precondition: administrator enter a valid administrator name and password
Post-condition: administrator can delete from the system.
Main success Scenario:
User selects to delete
1. Administrator enters username and password
2. Administrator clicks ‘delete’ button
3. System retrieves administrator name and password
4. System verifies the delete administrator information
5. System deletes the administrator information
6. System deletes administrator from the list of online administrator
7. System displays message “administrator Deleted”
8. System updates the main view.
2a. Extensions: System detects invalid delete:
1. System display “invalid administrator delete” message

 */

package ucm;

/**
 *
 * @author Mark
 */
import model.UsersModel;
import model.DatabaseModel;
import view.deleteAdminster;

import javax.swing.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

public class UCDeleteAdmin implements UCController {

    private UsersModel user;

    public UCDeleteAdmin() {
        user = new UsersModel("", "", "", "", "");
    }

    public UCDeleteAdmin(UsersModel user) {
        // System retrieves username and password
        this.user = user;
    }

    public UCDeleteAdmin(deleteAdminster deleteAdminster) {
        // System retrieves username and password
        this.user = deleteAdminster.getUserModel();
    }

    // STEP 1 - User Selects Delete
    public boolean run() {
        boolean deleteSuccessful = false;
        // STEP 2 - System retrieves username and password
        // This step is integrated into the constructor or
        // may be specified by using run(UserModel)
        // STEP 3 - System validates the username and password
        if (validUser()) {
            //Step 4 - System deletes user from the database
            if (deleteUser()) {
                //Step 5 - System displays 'user deleted' message
                JOptionPane.showMessageDialog(new JFrame(),
                        "Admin Deleted Successfully",
                        "Success",
                        JOptionPane.INFORMATION_MESSAGE);
                deleteSuccessful = true;
                new UCDispalyTicketsaler();
            } else {
                JOptionPane.showMessageDialog(new JFrame(),
                        "Deletion failed for unknown reason",
                        "Error Message",
                        JOptionPane.ERROR_MESSAGE);
                new UCDisplayDeleteAdmin();
            }
        } else {
            // STEP 2a - System displays error message
            JOptionPane.showMessageDialog(new JFrame(),
                    "Invalid Username or Password. Try again.",
                    "Error Message",
                    JOptionPane.ERROR_MESSAGE);
            new UCDisplayDeleteAdmin();
        }
        return deleteSuccessful;
    }

    // STEP 1 - User Selects Delete
    public void run(UsersModel user) {
        // STEP 2 - System retrieves username and password
        this.user = user;
        // STEP 3 onwards
        run();
    }

    private boolean validUser() {
        boolean correct = false;
        String username = user.getUsername();
        String password = user.getPassword();

        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet res = stmt.executeQuery("SELECT * FROM admins WHERE " +
                    "aname = '" + username + "'");

            correct = res.first() && username.equals(res.getString("aname"))
                    && password.equals(res.getString("apassword"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        return correct;
    }

    private boolean deleteUser() {
        boolean delete = false;
        String username = user.getUsername();
        Connection con = DatabaseModel.getInstance().getConnection();
        try {
            Statement stmt = con.createStatement(ResultSet.CONCUR_UPDATABLE,
                    ResultSet.CONCUR_UPDATABLE);

            stmt.executeUpdate("DELETE FROM admins WHERE aname = '"
                    + username + "'");

            ResultSet res = stmt.executeQuery("SELECT * FROM admins " +
                    "WHERE aname = '" + username + "'");

            delete = !(res.first() && username.equals(res.getString("aname")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return delete;
    }
}



